/// @file analyser.cpp
/// @brief Implementation of the Analyser class
/// @date 23/09/2009
/// @author Jérôme Stadelmann, Reds, Heig-vd
/// modified by Cédric Bardet, Reds, Heig-vd

#include "analyser.h"

//*************************************************************************************************
// Constructor
/// @param fftPoints Number of points in the frequency domain used to categorize the activity
//*************************************************************************************************
Analyser::Analyser(int fftPoints): m_fftPoints(fftPoints)
{


    for (int i=0; i<PROFILE_NUMBER; i++)
    {
        m_pMeans[i] = new float[SENSOR_CHANNEL_NUMBER * m_fftPoints];
        m_pStdDevs[i] = new float[SENSOR_CHANNEL_NUMBER * m_fftPoints];
    }
}

//*************************************************************************************************
// Destructor
//*************************************************************************************************
Analyser::~Analyser()
{


    for (int i=0; i<PROFILE_NUMBER; i++)
    {
        delete [] m_pMeans[i];
        delete [] m_pStdDevs[i];
    }

}

//*************************************************************************************************
// Function setView
/// @param pView A MainWindow object
//*************************************************************************************************
void Analyser::setView(sensorPlot* pView)
{

    m_pView = pView;
    connect(m_pView, SIGNAL(printAction(sensorPlot::graphData*,bool)), this, SLOT(onPrintAction(sensorPlot::graphData*, bool)));

}

//*************************************************************************************************
// Function addSensor
/// @param pSensor A pointer to the sensor to add
//*************************************************************************************************
void Analyser::addSensor(OakSensorVisual* pSensor)
{
    m_sensors.push_back(pSensor);

    // Extends the memory needed
    for (int i=0; i<PROFILE_NUMBER; i++)
    {
        m_pMeans[i] = (float*) realloc(m_pMeans[i], SENSOR_CHANNEL_NUMBER * m_fftPoints * m_sensors.size() * sizeof(float));
        m_pStdDevs[i] = (float*) realloc(m_pStdDevs[i], SENSOR_CHANNEL_NUMBER * m_fftPoints * m_sensors.size() * sizeof(float));
    }
}

//*************************************************************************************************
// Function logSensorData
//*************************************************************************************************
void Analyser::logSensorData()
{
    short array[SAMPLES_SIZE];

    for (unsigned int sensor=0; sensor<m_sensors.size(); sensor++)
    {
        for (int ch=0; ch<=SENSOR_CHANNEL_NUMBER; ch++)
        {
            int nPoints = m_sensors[sensor]->getBufferArray(ch, array, SAMPLES_SIZE, true);
            m_pLogSerializer->writeDataSet(array, nPoints);
        }
    }
}



//*************************************************************************************************
// Function onPrintAction
/// @param gData class content array of time, x, y  and z
/// @param print Is true if timer of sensorplot is running, else false
//*************************************************************************************************
void Analyser::onPrintAction(sensorPlot::graphData *gData,bool print)
{

    short array[nbr];

    if (print)
    {
        // init the "time array"
  for (int i=0;i<nbr;i++){
        gData->time[i]=i;
    }


   //get values for x y z and adapt the value in m/s^2
    for (unsigned int sensor=0; sensor<m_sensors.size(); sensor++)
     {
            m_sensors[sensor]->getBufferArray(1, array, nbr);

            for (int i=0; i<nbr; i++)
                gData->x[i] = (array[i]*(0.001));

            m_sensors[sensor]->getBufferArray(2, array, nbr);

			for (int i=0; i<nbr; i++)
                gData->y[i] = (array[i]*(0.001));

            m_sensors[sensor]->getBufferArray(3, array, nbr);

            for (int i=0; i<nbr; i++)
                gData->z[i] = (array[i]*(0.001));

      }

    }

}
